package com.example.demo.threadCommunication;

import java.io.Serializable;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorDemo {

    private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 30, 30, TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(2000), new ThreadPoolExecutor.CallerRunsPolicy());

    public static void main(String[] args) {

        int poolSize = threadPoolExecutor.getCorePoolSize();
        System.out.println("threadPoolExecutor's poolSize = " + poolSize);
        for (int i = 0; i < 100; i++) {
            threadPoolExecutor.execute(new Task());
            int poolSize2 = threadPoolExecutor.getPoolSize();
            int size = threadPoolExecutor.getQueue().size();
            System.out.println("threadPoolExecutor's poolSize = " + poolSize2);
            System.out.println("threadPoolExecutor's queueSize = " + size);
        }
        int poolSize3 = threadPoolExecutor.getPoolSize();
        int size2 = threadPoolExecutor.getQueue().size();
        System.out.println("threadPoolExecutor's poolSize = " + poolSize3);
        System.out.println("threadPoolExecutor's queueSize = " + size2);
    }

    static class Task implements Runnable, Serializable {

        @Override
        public void run() {
            System.out.println("Task is running......");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
